/*
 * @Author: 89-83-77
 * @Description: 路由配置文件
 * @Date: 2025-03-27 16:52:35
 * @LastEditTime: 2025-03-29 16:35:22
 * @FilePath: \fz-yzt-webd:\work\client\src\router\index.js
 */
import { createRouter, createWebHistory } from "vue-router";
// 添加正确的store引用
import { useUserStore } from "@/stores/user.js";
import { useRoleStore } from "@/stores/role.js";
// 引入布局组件
import MainLayout from "@/views/layouts/mainLayout.vue";
import LoginLayout from "@/views/layouts/loginLayout.vue";

// 路由配置说明
// {
//         path: "", // 路由路径
//         name: "", // 路由名称
//         meta: {
//           title: "数据概览", //菜单名称
//           icon: "PieChart", //菜单图标
//           requiresAuth: true, // 是否需要登录权限
//           permissions: ["admin"], //角色权限标识
//           hidden: true, // 是否菜单显示
//         },
//         component: () => import(""),// 组件懒加载
//  },

const routes = [
  // 登录页面路由
  {
    path: "/login",
    component: LoginLayout,
    children: [
      {
        path: "",
        name: "Login",
        component: () => import("@/views/login/login.vue"),
      },
    ],
  },
  // 错误页面路由
  {
    path: "/403",
    name: "Error403",
    component: () => import("@/views/error/Error403.vue"),
  },
  // 主路由
  {
    path: "/",
    component: MainLayout,
    meta: {
      requiresAuth: true,
      permissions: ["admin"],
    },
    children: [
      {
        path: "/dashboard", // 路由路径
        name: "Dashboard", // 路由名称
        // 路由元信息
        meta: {
          title: "数据概览", //菜单名称
          icon: "PieChart", //菜单图标
          requiresAuth: true, // 是否需要登录权限
          permissions: ["admin"], //角色权限标识
          hidden: true, // 是否菜单显示
        },
        // 组件懒加载
        component: () => import("@/views/cockpit/dashboard.vue"),
      },
      {
        path: "/workbench",
        name: "Workbench",
        meta: {
          title: "工作台",
          icon: "Monitor",
          permissions: ["admin"],
          hidden: false,
        },
        component: () => import("@/views/workbench/workbench.vue"),
      },
      {
        path: "/system",
        meta: {
          title: "系统管理",
          icon: "Setting",
          permissions: ["admin"],
          hidden: false,
        },
        children: [
          {
            path: "user",
            name: "User",
            meta: {
              title: "用户管理",
              icon: "User",
              permissions: ["admin"],
              hidden: false,
            },
            component: () => import("@/views/system/user.vue"),
          },
          {
            path: "menu",
            name: "Menu",
            component: () => import("@/views/system/menu.vue"),
            meta: {
              title: "菜单管理",
              icon: "Menu",
              hidden: false,
            },
          },
          {
            path: "role",
            name: "Role",
            component: () => import("@/views/system/role.vue"),
            meta: {
              title: "角色管理",
              icon: "UserFilled",
              hidden: false,
            },
          },
        ],
      },
    ],
  },
];

// 创建路由实例
// 使用createRouter创建路由实例，并传入history和routes
const router = createRouter({
  history: createWebHistory(), // 使用HTML5 History模式
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore(); // 正确获取store实例
  const roleStore = useRoleStore(); // 正确获取store实例
  // 检查是否需要登录权限
  if (to.meta.requiresAuth) {
    if (!userStore.token) {
      // 如果没有token，跳转到登录页面
      return next("/login");
    }
    // 获取当前用户角色权限
    const currentRole = roleStore.roles.find((r) => r.name === userStore.role);
    // 如果没有权限，跳转到403页面
    const hasPermission = currentRole?.permissions?.includes(to.path);
    if (!hasPermission) {
      // 如果没有权限，跳转到403页面
      return next("/403");
    }
  }
  next();
});

export default router;
